package com.newtouch.xcd.cms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.newtouch.xcd.cms.service.CrmService;
import com.newtouch.xcd.cms.to.crm.CrmData;
import com.newtouch.xcd.cms.to.crm.CrmDistributionTrustBenefitsDetailItem;
import com.newtouch.xcd.cms.to.crm.CrmDistributionTrustBenefitsItem;
import com.newtouch.xcd.cms.to.crm.CrmFinancialProductData;
import com.newtouch.xcd.common.constants.RedisKeyDict;
import com.newtouch.xcd.common.to.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Service
public class CrmServiceImpl implements CrmService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据信托编号查找家族信托的
     *
     * @param xtNumber
     * @return
     */
    @Override
    public CrmData queryCrmDateByXtNumber(String xtNumber) {

        String key = RedisKeyDict.CrmCache.MY_FAMILY_TRUST_TRADE_DETAIL.getCode() + xtNumber;

        String value = this.stringRedisTemplate.opsForValue().get(key);
        if (StrUtil.isBlank(value)) {
            return null;
        }

        CrmData crmData = JSONUtil.toBean(value, CrmData.class);
        return crmData;

    }

    /**
     * 根据分配受益人、分配项目类型查询利益分配记录
     *
     * @param assignBeneficiaries 分配受益人
     * @param assignmentType      分配项目类型
     * @return
     */
    @Override
    public List<CrmDistributionTrustBenefitsDetailItem> queryDistributionTrustBenefitsDetailByAssignBeneficiariesAndAssignmentType(String assignBeneficiaries
            , String assignmentType, CrmData crmData) {

        List<CrmDistributionTrustBenefitsDetailItem> distributionTrustBenefitsDetailItems = new ArrayList<>();

        CrmDistributionTrustBenefitsItem crmDistributionTrustBenefitsItem = crmData.getDistributionTrustBenefits();
        if (crmDistributionTrustBenefitsItem == null) {
            return null;
        }

        List<CrmDistributionTrustBenefitsDetailItem> crmDistributionTrustBenefitsDetailItems = crmDistributionTrustBenefitsItem.getDistributionTrustBenefitsDetailList();
        if (CollUtil.isEmpty(crmDistributionTrustBenefitsDetailItems)) {
            return null;
        }

        for (CrmDistributionTrustBenefitsDetailItem crmDistributionTrustBenefitsDetailItem : crmDistributionTrustBenefitsDetailItems) {
            if (assignBeneficiaries.equals(crmDistributionTrustBenefitsDetailItem.getAssignBeneficiaries()) && assignmentType.equals(crmDistributionTrustBenefitsDetailItem.getAssignmentType())) {
                distributionTrustBenefitsDetailItems.add(crmDistributionTrustBenefitsDetailItem);
            }
        }


        return distributionTrustBenefitsDetailItems;
    }

    /**
     * 根据客户编号查找理财产品（对象）
     *
     * @param customerId
     * @return
     */
    @Override
    public CrmFinancialProductData queryFinancialProductByCustomerId(String customerId){

        String key = RedisKeyDict.CrmCache.MY_FINANCIAL_PRODUCTS_TRADE_DETAIL.getCode() + customerId;

        String value = this.stringRedisTemplate.opsForValue().get(key);
        if (StrUtil.isBlank(value)) {
            return null;
        }

        CrmFinancialProductData crmFinancialProductData = JSONUtil.toBean(value, CrmFinancialProductData.class);
        return crmFinancialProductData;

    }
}
